<?php
namespace app\admin\controller;
use think\Db;
use lib\Curl;
class Contactus extends Base{
    //广告图
    public function contactus_banner(){
        if(request()->isGet()){
            $param = input('get.');
            $data=Db::name('contactus_banner')->where('id',1)->find();
            $this->assign('data',$data);
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['title']=$param['title'];
            $data['banner']=$param['banner'];
            $data['title_en']=$param['title_en'];
            $res= Db::name('contactus_banner')->where(array('id'=>1))->update($data);
            if($res){
                return json(['code' => 200,'msg' => '更新成功']);
            }else{
                return json(['code' => 500,'msg' => '失败']);
            }
        }
    }
    public function message(){
        $keyword=input('get.keyword');
        $state=input('get.state');
        if (!empty(input('get.fromtime'))) {
            $fromtime=strtotime(input('get.fromtime'));
        }else{
            $fromtime=0;
        }
        if (!empty(input('get.totime'))) {
            $totime=strtotime(input('get.totime'))+86400;
        }else{
            $totime=time();
        }
        $map = [
            ['time', '>', $fromtime],
            ['time', '<', $totime]
        ];
        if($state==0){
            array_push($map,['state','<',4]);
        }else{
            array_push($map,['state','=',$state]);
        }
        $list=Db::name('message')->where($map)->where('name|tel|email|company','like',"%{$keyword}%")->order('id desc')->paginate(10);
        $lists=Db::name('message')->where($map)->where('name|tel|email|company','like',"%{$keyword}%")->count();
        $data=$list->all();
        $this->assign('keyword',input('get.keyword'));
        $this->assign('fromtime',input('get.fromtime'));
        $this->assign('totime',input('get.totime'));
        $this->assign('state',$state);
        $this->assign('data',$data);
        $this->assign('list',$list);
        $this->assign('lists',$lists);
        return $this->fetch();
    }
    public function state_ing(){
        $id=input('param.id');
        $status = Db::name('message')->where(array('id'=>$id))->update(['state'=>2]);
        if($status){
            return json(['code' => 200,'msg' => '进入处理中']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
    public function state_ed(){
        $id=input('param.id');
        $status = Db::name('message')->where(array('id'=>$id))->update(['state'=>3]);
        if($status){
            return json(['code' => 200,'msg' => '已处理']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
}